﻿using System;
using System.Collections.Generic;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Transactions;
using System.Web.Mvc;
using eNote.Models;
using Orchard.Themes;
using System.Linq;

namespace eNote.Controllers
{
    [Themed]
    [ServiceContract]
    public class NoteMainController : Controller
    {
        public ActionResult Index()
        {
            return GetNoteView("eNote");
        }

        public void SaveNote(Note t)
        {
            System.IO.File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", "eNote", t.FName), t.Text);
        }

        public PartialViewResult SaveNoteItem(NoteItem note)
        {
            var items = GetNoteItems(note.FName);
            if (note.State == State.Update)
            {
                var item = items.Single(n => n.Index == note.Index);
                item.Text = note.Text;
                SaveNoteItems(items, note.FName);

                return null;
            }
            if (note.State == State.Delete)
            {
                items.RemoveAll(i => i.Index == note.Index);
                SaveNoteItems(items, note.FName);

                return PartialView("NoteItemView", GetNoteItems(note.FName));
            }

            if (note.State == State.Insert)
            {
                items.Add(note);
                SaveNoteItems(items, note.FName);
                return PartialView("NoteItemView", GetNoteItems(note.FName));
            }

            throw new Exception("Invalid STATE");
        }

        public void SaveNoteItems(List<NoteItem> items, string f)
        {
            f = ResolveFName(f);
            var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", "eNote", f);
            var txt = "";
            for (int i = 0; i < items.Count; i++)
            {
                txt += items[i].Text;
                if (i != items.Count - 1)
                    txt += "\n";
            }

            System.IO.File.WriteAllText(file, txt);
        }

        public ActionResult GetNoteView(string f, bool flexMode = false)
        {
            f = ResolveFName(f);
            if (flexMode)
            {
                var items = GetNoteItems(f);

                return View("_eNoteFlexView", new NoteViewModel { FName = f, NoteItems = items });
            }
            return View("_eNoteMainView", GetNote(f));
        }

        static string ResolveFName(string f)
        {
            return f.Replace(".txt", "") + ".txt";
        }

        public List<NoteItem> GetNoteItems(string f)
        {
            var note = GetNote(f);
            return note.Text.Split('\n').Select((t, i) => new NoteItem { Index = i, Text = t, FName = note.FName }).ToList();
        }

        [WebGet(UriTemplate = "")]
        public Note GetNote(string f)
        {
            f = ResolveFName(f);
            var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", "eNote", f);
            if (!System.IO.File.Exists(file))
                System.IO.File.WriteAllText(file, f);
            var txt = System.IO.File.ReadAllText(file);
            return new Note { Text = txt, FName = f };
        }

        public JsonResult GetNoteJson(string f)
        {
            return Json(GetNote(f), JsonRequestBehavior.AllowGet);
        }

        public string GetNoteText(string f)
        {
            return GetNote(f).Text;
        }
    }
}
